package com.wtwd.campus.h5_manager.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.dao.DeviceClassPatternMapper;
import com.wtwd.campus.dao.WhiteListMapper;
import com.wtwd.campus.h5_manager.dao.H5DeviceManageMapper;
import com.wtwd.campus.h5_manager.dao.H5DeviceSettingMapper;
import com.wtwd.campus.h5_manager.entity.H5DeviceSetting;
import com.wtwd.campus.h5_manager.entity.H5Deviceinfo;
import com.wtwd.campus.h5_manager.service.H5DeviceManageService;
import com.wtwd.campus.service.DeviceFlagService;
import com.wtwd.campus.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author mjy
 * @date 2022/03/09
 * @description
 */
@Service
public class H5DeviceManageServiceImpl implements H5DeviceManageService {


    @Autowired
    private H5DeviceManageMapper deviceManageMapper;

    @Autowired
    private H5DeviceSettingMapper deviceSettingMapper;

    @Autowired
    private WhiteListMapper whiteListMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private DeviceClassPatternMapper deviceClassPatternMapper;

    @Resource
    private DeviceFlagService deviceFlagService;

    @Override
    public List<H5Deviceinfo> queryDeviceByConditions(H5Deviceinfo searchDevice) {
        List<H5Deviceinfo> deviceinfos = deviceManageMapper.queryDeviceByConditions(searchDevice);
        if (deviceinfos != null && deviceinfos.size() > 0) {
            deviceinfos.stream().forEach(e -> {
                boolean online = deviceOnline(e.getImei());
                e.setState(online ? 1 : 2);
            });
            if (searchDevice.getState() != null && searchDevice.getState() != 0) {
                deviceinfos = deviceinfos.stream().filter(e -> e.getState().equals(searchDevice.getState())).collect(Collectors.toList());
            }
        }
        return deviceinfos;
    }

    @Override
    public Boolean AuthImei(String imei) {
        H5Deviceinfo deviceinfo = this.getDeviceInfoByimei(imei);
        if (deviceinfo != null) {
            return false;
        }
        return true;
    }

    @Override
    public H5Deviceinfo getDeviceInfoByimei(String imei) {
        return new LambdaQueryChainWrapper<>(deviceManageMapper)
                .eq(H5Deviceinfo::getImei, imei)
                .one();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer addDeviceInfo(H5Deviceinfo h5DeviceInfo) {
        int rest = deviceManageMapper.insert(h5DeviceInfo);
        //添加设备的同时初始化设备设置表
        H5DeviceSetting h5DeviceSetting = new H5DeviceSetting();
        h5DeviceSetting.setDeviceId(h5DeviceInfo.getDeviceId());
        h5DeviceSetting.setImei(h5DeviceInfo.getImei());
        deviceSettingMapper.addDeviceSetting(h5DeviceSetting);
        //将缓存中的未入库imei删掉
        List<String> list = new ArrayList<>();
        list.add(h5DeviceInfo.getImei());
        redisService.deleteUnInStorageList(list);
        return rest;
    }

    @Override
    public H5Deviceinfo getDeviceInfoByDeviceId(Integer deviceId) {
        return new LambdaQueryChainWrapper<>(deviceManageMapper)
                .eq(H5Deviceinfo::getDeviceId, deviceId)
                .one();
    }

    @Override
    public Integer deleteDeviceById(Integer deviceId) {
        //删除白名单数据
        whiteListMapper.delDeviceAllWhiteListByDeviceId(deviceId, 0);
        //删除课堂模式数据
        deviceClassPatternMapper.delDeviceAllClassesPatternByDeviceId(deviceId);
        //删除设备设置表数据
        deviceSettingMapper.delDeviceSettingByDeviceId(deviceId);
        //删除SOS号码
        deviceSettingMapper.deleteSOSList(deviceId);
        //删除设备表数据
        return deviceManageMapper.deleteDeviceById(deviceId);
    }

    @Override
    public IPage<H5Deviceinfo> queryDeviceByConditionsPage(H5Deviceinfo searchDevice, Page<H5Deviceinfo> page) {
        IPage<H5Deviceinfo> device = deviceManageMapper.queryDeviceByConditionsPage(searchDevice, page);
        List<H5Deviceinfo> records = device.getRecords();
        List<String> imeis = new ArrayList<>();
        if (records!=null&&records.size()>0){
            imeis=records.stream().map(H5Deviceinfo::getImei).collect(Collectors.toList());
        }
        Map<String, Boolean> stringBooleanMap = deviceFlagService.bathGetDeviceOnline(imeis);
        records.stream().forEach(e -> {
            boolean online = stringBooleanMap.get(e.getImei());
            e.setState(online ? 1 : 2);
            if (e.getIfScreen()==null){
                e.setIfScreen(0);
            }
        });
        if (searchDevice.getState()!=null&&searchDevice.getState()!=0){
            records =records.stream().filter(e->e.getState().equals(searchDevice.getState())).collect(Collectors.toList());
        }
        return device;
    }

    @Override
    public IPage<H5Deviceinfo> queryVcomDeviceByConditionsPage(H5Deviceinfo searchDevice, Page<H5Deviceinfo> page) {
        IPage<H5Deviceinfo> device = deviceManageMapper.queryVcomDeviceByConditionsPage(searchDevice, page);
        List<H5Deviceinfo> records = device.getRecords();
        List<String> imeis = new ArrayList<>();
        if (records!=null&&records.size()>0){
            imeis=records.stream().map(H5Deviceinfo::getImei).collect(Collectors.toList());
        }
        Map<String, Boolean> stringBooleanMap = deviceFlagService.bathGetDeviceOnline(imeis);
        records.stream().forEach(e -> {
            boolean online = stringBooleanMap.get(e.getImei());
            e.setState(online ? 1 : 2);
            if (e.getIfScreen()==null){
                e.setIfScreen(0);
            }
        });
        if (searchDevice.getState()!=null&&searchDevice.getState()!=0){
            records =records.stream().filter(e->e.getState().equals(searchDevice.getState())).collect(Collectors.toList());
        }
        return device;
    }


    /**
     * 判断设备是否在线
     *
     * @param imei 设备imei号
     * @return 设备是否在线
     */
    public boolean deviceOnline(String imei) {
        boolean isOnline = redisService.hasKey(Constants.DEVICE_ONLINE_PREFIX + imei);
        return isOnline;
    }
}
